package com.probiz.estore.system.service.impl;

import java.util.List;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.system.PaymentGateway;
import com.probiz.estore.common.model.system.PaymentMethod;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.system.dao.PaymentMethodDao;
import com.probiz.estore.system.service.PaymentGatewayManager;

/**
 * PaymentMethod Business Delegate (Proxy) implementation to handle communication between web and
 * persistence layer. Implementation of PaymentMethodManager interface. Developer introduced
 * interfaces should be declared here. Won't get overwritten.
 */
public class PaymentMethodManagerImpl extends GenericManagerImpl<PaymentMethod> implements com.probiz.estore.system.service.PaymentMethodManager {

	private PaymentGatewayManager paymentGatewayManager = null;

	private PaymentMethodDao paymentMethodDao = null;

	public void setPaymentGatewayManager(PaymentGatewayManager paymentGatewayManager) {
		this.paymentGatewayManager = paymentGatewayManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.system.service.PaymentMethodManager#addPaymentMethod(java.lang.Integer)
	 */
	public PaymentMethod addPaymentMethod(Integer paymentGatewayId) {
		PaymentMethod paymentMethod = paymentMethodDao.getByPaymentGatewayId(paymentGatewayId);
		if (null == paymentMethod) {
			paymentMethod = new PaymentMethod();
		}	
		PaymentGateway paymentGateway = paymentGatewayManager.getById(paymentGatewayId);
		paymentMethod.setPaymentMethodName(paymentGateway.getPaymentGatewayName());
		paymentMethod.setPaymentMethodDetail(paymentGateway.getPaymentGatewayDetail());
		paymentMethod.setProtocol("HTTP");
		paymentMethod.setPaymentMethodType(PaymentMethod.PAYMENT_METHOD_TYPE_ONLINE);// online
		paymentMethod.setStatus(Constants.STATUS_INACTIVE);// inactive
		paymentMethod.setIscod(Constants.FLAG_FALSE);
		paymentMethod.setSortOrder(Integer.valueOf(paymentMethodDao.getMaxSortOrder().intValue() + 1));
		paymentMethod.setPaymentGateway(paymentGateway);
		this.save(paymentMethod);
		paymentGatewayManager.updateIsAddedStatus(paymentGatewayId, PaymentGateway.ADDED);
		return paymentMethod;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.PaymentMethodManager#removePaymentMethod(com.probiz.estore.common.model.system.PaymentMethod)
	 */
	public void removePaymentMethod(PaymentMethod paymentMethod){
		// set the payment method to delete status
		paymentMethodDao.updatePaymentMethodStatus(paymentMethod.getPaymentMethodId(), Constants.STATUS_DELETED);
		// set the payment gateway to unAdded status
		paymentGatewayManager.updateIsAddedStatus(paymentMethod.getPaymentGatewayId(), PaymentGateway.UNADDED);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.PaymentMethodManager#updateBatchPaymentMethods(java.util.List)
	 */
	public void updateBatchPaymentMethods(List<PaymentMethod> paymentMethodList) {
		for(PaymentMethod paymentMethod: paymentMethodList){
			this.save(paymentMethod);
		}
	}

	/**
	 * get all paymentMethod ,but not include the delete paymentMethod.
	 */
	public List<PaymentMethod> findUnDeletedPaymentMethods() {
		return paymentMethodDao.findAllByStatus("(" + Constants.STATUS_ACTIVE + "," + Constants.STATUS_INACTIVE + ")");
	}

	/**
	 * return List By payment method type
	 * 
	 * @param paymentType
	 */
	public List<PaymentMethod> getByPaymentMethodType(Short paymentType) {
		return paymentMethodDao.getByPaymentMethodType(paymentType);
	}

	/**
	 * return List by payment method type and status
	 * 
	 * @param paymentMethodType
	 * @param status
	 */
	public List<PaymentMethod> getByPaymentMethodTypeStatus(Short paymentMethodType, Short status) {
		return paymentMethodDao.getByPaymentMethodTypeStatus(paymentMethodType, status);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.PaymentMethodManager#getByPaymentGatewayId(java.lang.Integer)
	 */
	public PaymentMethod getByPaymentGatewayId(Integer paymentGatewayId) {
		return paymentMethodDao.getByPaymentGatewayId(paymentGatewayId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.PaymentMethodManager#getByPaymentGatewayName(java.lang.String)
	 */
	public PaymentMethod getByPaymentGatewayName(String paymentGatewayName){
		return (PaymentMethod)paymentMethodDao.findUnique("from PaymentMethod pm where pm.paymentGateway.paymentGatewayName like ?", "%"+paymentGatewayName+"%");
	}

	public List<PaymentMethod> getOnlinePaymentMethodByPaymentGatewayTypeStatus(Short paymentGatewayType, Short status) {
		return paymentMethodDao.getOnlinePaymentMethodByPaymentGatewayTypeStatus(paymentGatewayType, status);
	}

	@Override
	protected void initManager() {
		super.dao = paymentMethodDao;
	}

	@Override
	protected void preDelete(PaymentMethod entity) {

	}

	@Override
	protected void preSave(PaymentMethod entity) {

	}

	public void setPaymentMethodDao(PaymentMethodDao paymentMethodDao) {
		this.paymentMethodDao = paymentMethodDao;
	}
}